<template>
    <Form ref="form" :model="form" :rules="ruleValidate" :label-width="120">
        <FormItem label="选择值班项" prop="watchlist_id">
            <Select v-model="form.watchlist_id" :disabled="disabled" placeholder="请选择值班项">
                <Option v-for="item in edu" :value="item.watchlist_id" :key="item.watchlist_id">{{ item.watchlist_name.zh?item.watchlist_name.zh:item.watchlist_name.mn }}</Option>
            </Select>
        </FormItem>
        <FormItem label="分类" prop="classification">
            <RadioGroup v-model="form.classification">
                <Radio label="1">加分</Radio>
                <Radio label="2">减分</Radio>
            </RadioGroup>
        </FormItem>
        <FormItem label="固定分值" prop="fixed_score">
            <Input v-model="form.fixed_score" placeholder="请输入固定分值"></Input>
        </FormItem>
        <FormItem label="酌情分最低分值" prop="lowest_score">
            <InputNumber v-model="form.lowest_score"></InputNumber>
        </FormItem>
        <FormItem label="酌情分最高分值" prop="highest_score">
            <InputNumber v-model="form.highest_score"></InputNumber>
        </FormItem>
        <FormItem label="内容" prop="entry_content.zh">
            <Input v-model="form.entry_content.zh" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="输入内容"></Input>
        </FormItem>
        <FormItem label="说明" prop="remarks.zh">
            <Input v-model="form.remarks.zh" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="输入说明"></Input>            
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit('form')">{{btnTitle}}</Button>
        </FormItem>
    </Form>
</template>

<script>
export default {
    props: ['items','condition'],
    data () {
        return {
            btnTitle:null,
            disabled:false,
            form:{
                entry_content: {
                    zh:null,
                    en:null,
                    mn:null,
                },
                remarks: {
                    zh:null,
                    en:null,
                    mn:null,
                },
                classification:'2',
                highest_score: 0,
                lowest_score: 0,
                fixed_score: null,
                watchlist_id: null,
            },
            addUrl:'api/watchlist/v1/watchlistrules/add',
            editUrl:'api/watchlist/v1/watchlistrules/edit/' + this.condition,
            selUrl:'api/watchlist/v1/watchlistrules/find/' + this.condition,
            ruleValidate: {
                watchlist_id: [
                    { required: true, type: 'number', message: '请选择值班项', trigger: 'change' }
                ],
                'entry_content.zh':[
                    { required: true, message: '请输入内容', trigger: 'blur' }
                ],
                'remarks.zh':[
                    { required: true, message: '请输入说明', trigger: 'blur' }
                ],
                classification: [
                    { required: true, message: '请选择分类', trigger: 'change' }
                ],
                lowest_score:[
                    { required: true, type: 'number', message: '请输入酌情分最低分值', trigger: 'blur' },
                ],
                highest_score:[
                    { required: true, type: 'number', message: '请输入酌情分最高分值', trigger: 'blur' },
                ],
            },

            edu:Array,
            eduUrl:'api/watchlist/v1/get/watchlistclass/list',
        }
    },
    methods: {
        handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    this.submit()
                } else {
                    this.$Message.error('请检查填写内容');
                }
            })
        },
        submit () {
            if(this.condition !=null){
                this.$http.post(this.addUrl,this.form).then(val=>{
                    this.promptBox = false;
                    this.$emit('requery',true)
                }).catch(err=>{
                    if(err.code && err.code==422) {
                        this.$Message.error(err.message);
                    }
                })
            }else{
                this.$http.put(this.editUrl,this.form).then(val=>{
                    this.promptBox = false;
                    this.$emit('requery',true)
                }).catch(err=>{
                    if(err.code && err.code==422) {
                        this.$Message.error(err.message);
                    }
                })
            }
            
        },
        eduData(){
            this.$http.get(this.eduUrl).then(val=>{
                this.edu = val.data
            }).catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.error(err.message);
                }
            })
        },
        selData(){
            this.$http.get(this.selUrl).then(val=>{
                let singleData = JSON.parse(JSON.stringify(val.data))
                this.form.entry_content = singleData.entry_content
                this.form.remarks = singleData.remarks
                this.form.fixed_score = singleData.fixed_score
                this.form.highest_score = parseFloat(singleData.highest_score)
                this.form.lowest_score = parseFloat(singleData.lowest_score)
                this.form.classification = singleData.classification
                this.form.watchlist_id = parseInt(singleData.watchlist_id)
            }).catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.error(err.message);
                }
            })
        },
    },
    mounted(){
        if(this.items){
            console.log(this.items)
            this.form.watchlist_id = this.items
            // this.disabled = true
        }else{
            this.disabled = false
        }
        this.eduData()
        if(this.condition !=null){
            this.selData()
            this.btnTitle = '修改'
        }else{
            this.btnTitle = '添加'
        }
    },
}
</script>